package me.pearjelly.util;

import java.util.Collection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import me.pearjelly.model.Category;
import me.pearjelly.model.Pic;
import me.pearjelly.model.PicGroup;
import me.pearjelly.model.Selector;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class SourceParseUtil {
	public static String[] parseTexts(String html, String[] querys,
			String[] regs) {
		if (querys != null) {
			Document doc = Jsoup.parse(html);
			String[] texts = new String[querys.length];
			for (int i = 0; i < querys.length; i++) {
				String query = querys[i];
				texts[i] = doc.select(query).first().text();
				if (null != regs && regs.length > i && null != regs[i]) {
					String reg = regs[i];
					Pattern pattern = Pattern.compile(reg);
					Matcher matcher = pattern.matcher(texts[i]);
					if (matcher.matches()) {
						texts[i] = matcher.group(1);
					}
				}
			}
		}
		return null;
	}

	public static String parseText(String html, String query, String reg) {
		if (null != query) {
			Document doc = Jsoup.parse(html);
			String text = doc.select(query).first().text();
			if (null != reg) {
				Pattern pattern = Pattern.compile(reg);
				Matcher matcher = pattern.matcher(text);
				if (matcher.matches()) {
					text = matcher.group(1);
				}
			}
			return text;
		}
		return null;
	}

	public static void parsePic(String html, Collection<Selector> selectors,
			Pic pic) {
		Document doc = Jsoup.parse(html);
		String entityName = pic.getClass().getName();
		for (Selector selector : selectors) {
			if (entityName.equals(selector.getEntityName())) {
				String text = doc.select(selector.getQuery()).text();
				String reg = selector.getReg();
				if (null != reg && reg.length() > 0) {
					Pattern pattern = Pattern.compile(reg);
					Matcher matcher = pattern.matcher(text);
					if (matcher.matches()) {
						text = matcher.group(1);
					}
				}
				if (text != null && text.length() > 0) {
					String propertyName = selector.getPropertyName();
					if ("name".equals(propertyName)) {
						pic.setName(text);
					} else if ("keywords".equals(propertyName)) {
						pic.setTags(text);
					} else if ("description".equals(propertyName)) {
						pic.setDesc(text);
					}
				}
			}
		}
	}

	public static void parsePicGroup(String html,
			Collection<Selector> selectors, PicGroup picGroup) {
		Document doc = Jsoup.parse(html);
		String entityName = picGroup.getClass().getName();
		for (Selector selector : selectors) {
			if (entityName.equals(selector.getEntityName())) {
				String text = doc.select(selector.getQuery()).text();
				String reg = selector.getReg();
				if (null != reg && reg.length() > 0) {
					Pattern pattern = Pattern.compile(reg);
					Matcher matcher = pattern.matcher(text);
					if (matcher.matches()) {
						text = matcher.group(1);
					}
				}
				if (text != null && text.length() > 0) {
					String propertyName = selector.getPropertyName();
					if ("name".equals(propertyName)) {
						picGroup.setName(text);
					} else if ("keywords".equals(propertyName)) {
						picGroup.setTags(text);
						picGroup.setKeywords(text);
					} else if ("description".equals(propertyName)) {
						picGroup.setDesc(text);
					}
				}
			}
		}
	}

	public static Long choosePicGroupId(PicGroup picGroup,
			Collection<Category> categories, Long defaultCategoryId) {
		if (picGroup != null) {
			Category unmatch = null;
			for (Category category : categories) {
				if (category != null) {
					unmatch = unmatch == null
							|| unmatch.getPriority() < category.getPriority() ? category
							: unmatch;
					if (category.getKeywords() == null) {
						continue;
					}
					String[] keywords = category.getKeywords().split("[,]");
					for (String keyword : keywords) {
						if (PicGroup.getAllWords(picGroup).contains(keyword)) {
							return category.getId();
						}
					}
				}
			}
			if (null != unmatch) {
				return unmatch.getId();
			}
		}
		return defaultCategoryId;
	}
}
